package cn.fl.capital.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.upload.api.IUploadApiService;
import cmm.comm.facade.upload.condition.SysUlDatCfgQueryCondition;
import cmm.comm.facade.upload.dto.SysUlDatCfgDTO;
import cmm.comm.facade.upload.dto.SysUlDatCfgDtlDTO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.capital.condition.CapFileInfoQueryCondition;
import cn.fl.capital.constant.CapFileInfoConstant;
import cn.fl.capital.constant.CapitalConstant;
import cn.fl.capital.service.ICapFileInfoService;
import cn.fl.capital.vo.CapFileInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.constant.PrjFileInfoConstant;
import cn.fl.upload.constant.UploadConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CapFileInfoServiceImpl implements ICapFileInfoService {

    private static final Logger logger = LoggerFactory.getLogger(CapFileInfoServiceImpl.class);

    @Autowired
    private IUploadApiService uploadApiService;

    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

    @Override
    public List<CapFileInfoVO> searchCapFundFileInfoList(CapFileInfoQueryCondition condition) throws BaseException {
        return this.getUploadFileList(condition);
    }

    @Override
    public boolean removeFile(Long fileId) throws BaseException {
        List<Long> idList = new ArrayList<>(1);
        idList.add(fileId);
        return this.uploadApiService.removeSysUlFilesRecByIds(idList);
    }

    private List<CapFileInfoVO> getUploadFileList(CapFileInfoQueryCondition condition) throws BaseException {
        //查询资料配置，系统代码,组织架构代码,配置编号 ,业务主键
        SysUlDatCfgQueryCondition ulDatCfgQueryCondition = new SysUlDatCfgQueryCondition();
        ulDatCfgQueryCondition.setSysCd(CurrentThreadContext.getCurrentSysCd());//系统代码
        if (StringUtils.isNotEmpty(condition.getCstMgrOrgCd())) {
            ulDatCfgQueryCondition.setOrgCd(condition.getCstMgrOrgCd());//客户经理组织架构代码
        } else {
            ulDatCfgQueryCondition.setOrgCd(CapitalConstant.SYSORG);
        }

        ulDatCfgQueryCondition.setDataId(String.valueOf(condition.getDataId()));//业务主键
        List<String> cfgCdList = new ArrayList<>();
        cfgCdList.add(condition.getCfgCd());//配置编号
        ulDatCfgQueryCondition.setCfgCdList(cfgCdList);//配置编号集合
        List<SysUlDatCfgDTO> list = uploadApiService.searchSysUlDatCfgListByDataId(ulDatCfgQueryCondition);
        List<CapFileInfoVO> voList = null;
        if (CollectionUtils.isNotEmpty(list)) {
            Map<String, String> audStsCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), CapitalConstant.CODE_MATE_AUD_STS_CD);
            voList = new ArrayList<>();
            CapFileInfoVO vo;
            //设置路径
            String picServicePath = PropertiesCacheUtils.getProperty(UploadConstant.UPLOAD_SERVER_PATH);
            for (SysUlDatCfgDTO dto : list) {
                for (SysUlDatCfgDtlDTO dtlDTO : dto.getSysUlDatCfgDtlDTOList()) {
                    vo = new CapFileInfoVO();
                    vo.setId(condition.getDataId());//业务主键
                    vo.setCfgDtlCd(dtlDTO.getCfgDtlCd());//配置子编号
                    vo.setDatNm(dtlDTO.getDatNm());//配置名称
                    vo.setCfgCd(dtlDTO.getCfgCd());//配置编号
                    vo.setOrgCd(dtlDTO.getOrgCd());//配置组织架构代码
                    vo.setCfgNm(dto.getCfgNm());//配置子名称
                    vo.setFileId(dtlDTO.getRecId());//附件ID
                    //如果有HTTP开头说明是可以访问路径,无需干涉
                    if (StringUtils.isNotBlank(dtlDTO.getFilePath()) && !dtlDTO.getFilePath().startsWith("http") && StringUtils.isNotBlank(picServicePath)) {
                        vo.setFilePath(picServicePath + dtlDTO.getFilePath());
                    } else {
                        vo.setFilePath(dtlDTO.getFilePath());
                    }
                    vo.setFileNm(dtlDTO.getRecFileNm());//上传名称
                    vo.setCrtTm(dtlDTO.getRecCrtTm());//上传时间
                    vo.setIsMust(dtlDTO.getIsMust());//是否必传
                    if (StringUtils.isNotBlank(dtlDTO.getRecAudStsCd())) {
                        vo.setAudStsCd(dtlDTO.getRecAudStsCd());//验证状态
                    } else {
                        vo.setAudStsCd("0");//验证状态
                    }

                    if (audStsCdMap != null && audStsCdMap.containsKey(vo.getAudStsCd())) {
                        vo.setAudStsCdNm(audStsCdMap.get(vo.getAudStsCd()));
                    }
                    voList.add(vo);
                }
            }
        }
        return voList;
    }
}
